﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;

namespace com.dz.ct.common
{
    public class AppConfig
    {
        /// <summary>
        /// sql连接字符串
        /// </summary>
        public static string SqlConnectionString
        {
            get { return GetConfig("SqlConnectionString", @"Data Source=.;Initial Catalog=BookInstance;User ID=sa;Password=123456"); }
        }

        /// <summary>
        /// MySQL数据库链接
        /// </summary>
        public static string MySQLConnectionString
        {
            get { return GetConfig("MySQLConnectionString", @"server=127.0.0.1;user id=root; password=tyh2004111091; database=mysql"); }
        }

        /// <summary>
        /// 数据库名字
        /// </summary>
        public static string DbName
        {
            get { return GetConfig("DbName", @"tiyma"); }
        }

        /// <summary>
        /// 存放书章节的根目录
        /// </summary>
        public static string BookChapterRoot
        {
            get
            {
                string t_path = string.Empty;
                if (string.IsNullOrEmpty(GetConfig("BookChapterRoot", "")))
                {
                    t_path = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    t_path = GetConfig("BookChapterRoot", "");
                }
                return t_path;
            }
        }

        /// <summary>
        /// 章节的保存形式 0为文本 1 为数据库
        /// </summary>
        public static int ChapterType
        {
            get
            {
                int t_chapter_type = 0;
                if (!int.TryParse(GetConfig("ChapterType", "0"), out t_chapter_type))
                    t_chapter_type = 0;
                return t_chapter_type;
            }
        }

        
        #region tools
        private static NameValueCollection _settings = null;

        public static NameValueCollection Settings
        {
            get { return AppConfig._settings; }
            set { AppConfig._settings = value; }
        }



        private static string GetConfig(string key, string defaultValue)
        {

            if (_settings == null)
                _settings = ConfigurationManager.AppSettings;

            if (Settings[key] != null)
                return Settings[key];
            else
                return defaultValue;

        }
        #endregion
    }
}
